*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	Identifies the variables to be used for VAM testing:
*		0. drops observations missing the outcome
*		1. `Y' contains the outcome
*		2. `Z' contains offer instruments
*		3. `D' contains enrollment dummies
*		4. `P' contains list of propensity bl_scores
*		5. `S' contains a lottery sample indicator
*		6. `Y'_gain is a variable that equals `Y' minus the relevant baseline score
*
*	OPTIONS
*		y - "m" for math or "e" for ela
*		ptype - type of propensity scores: "form", "freq", "iform", "ifreq"
*	----------------------------------------------------------------------------

	program define ivset, rclass
	cap program drop ivset
	syntax, [y(name)] [ptype(name)] [testgrade(name)] [bin]

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	//parse
	if "`bin'" != "" local bin bin
	if "`testgrade'" == "9" local testgrade

	//outcome
	if "`y'"!=""{
		if "`y'" == "m" local Y math`testgrade'
		if "`y'" == "e" local Y ela`testgrade'
		if "`y'" == "c" local Y total`testgrade'

		//gains outcome
		if "`y'" == "m" cap gen `Y'_gain = `Y' - bl_math
		if "`y'" == "e" cap gen `Y'_gain = `Y' - bl_ela
		if "`y'" == "c" cap gen `Y'_gain = `Y' - bl_total
		if _rc != 0 display "ivset: no gains outcome generated"

		drop if mi(`Y')
		return local Y `Y'
	}

	//endog
	return local D		enr_????

	if "`ptype'"!=""{
		//instruments & pscores
		if "`bin'"==""{
			local Z
			local P
			qui levelsof sch if lottery_`ptype'_sch, local(lottoschools)
			foreach sch in `lottoschools'{
				local Z `Z' offer_`sch'
			}
			qui levelsof sch, local(allschools)
			foreach sch in `allschools'{
				cap confirm numeric variable pscore_`ptype'_`sch'
				if _rc == 0{
					if strpos("`ptype'","i") == 0  local P `P' pscore_`ptype'_`sch' pscore_`ptype'_`sch'_0
					else local P `P' i.pscore_`ptype'_`sch'
				}
			}
		}
		else{
			local Z `bin'offer_*
			if strpos("`ptype'","i") == 0  local P `bin'pscore_`ptype'_*
			else local P i.`bin'pscore_`ptype'_*
		}
		if strpos("`ptype'","2") > 0 local P `P' i.year#c.rv_*

		return local Z 		`Z'
		return local P 		`P'

		//sample
		local type = subinstr("`ptype'","i","",1)
		return local S		`bin'lottery_`type'_sample
	}

	end
